<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 超值选购ajax类
 * @package  ajax
 * @subpackage  backend
 * @author   create by allen.qiang
 */

require 'module/base/promotionHandler.class.php';

class ajax_selection {
	//入口
	public function execute(){
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		switch ($operFlg){
			case 1://显示超值选购列表
				$this->showSelectionList();
				break;
			case 2:
			case 3://超值选购编辑
				$this->editSelection();
				break;
			case 4://删除超值选购
				$this->deleteSelection();
				break;
			case 5://根据输入的商品编号得到商品信息
				$this->getGoodsInfo();
				break;
			case 6://启用禁用
				$this->changeUseFlg();
				break;
            case 7:
                $this->getSelectInfo();
                break;
			default:
				echo json_encode(array('code'=>400,'message'=>'operFlg传值错误'));
				break;
		}
	}
	
	//显示超值选购列表
	private function showSelectionList (){
		global $G_PGLOB;
		//当前页
		$page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;
		
		//每页显示条数
		$rp = isset($_REQUEST['rp']) ? $_REQUEST['rp'] : 10;
		
		//排序的列的名称
		$sortname = isset($_REQUEST['sortname']) ? $_REQUEST['sortname'] : 'goodsChoiceID';
		
		//排序方式（正序、倒序）
		$sortorder = isset($_REQUEST['sortorder']) ? $_REQUEST['sortorder'] : 'ASC';
		
		//活动号
		$goodsChoiceID = isset($_REQUEST['goodsChoiceID']) ? $_REQUEST['goodsChoiceID'] : '';
		
		//活动名称
		$goodsChoiceName = isset($_REQUEST['goodsChoiceName']) ? $_REQUEST['goodsChoiceName'] : '';
		
		//所属分类
		$itemName = isset($_REQUEST['itemName']) ? $_REQUEST['itemName'] : '';
		
		//活动日期
		$date = isset($_REQUEST['date']) ? $_REQUEST['date'] : '';
		
		//活动号（用来查询选购商品）
		$choice_id = (isset($_REQUEST['choice_id']) && is_numeric($_REQUEST['choice_id'])) ? $_REQUEST['choice_id'] : '';
		
		$importData = array('page' 				=> $page,
							'rp'                => $rp,
							'sortname'          => $sortname,
							'sortorder'         => $sortorder,
							'goodsChoiceID'		=> $goodsChoiceID,
							'goodsChoiceName'	=> $goodsChoiceName,
							'itemName'			=> $itemName,
							'date'				=> $date);
		
		$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
		
		$result_selection = $promotionHandler->searchSelection( $importData );
		
		if (!empty($choice_id)){
				
			$commHandler = new commHandler($G_PGLOB->DBCA);
				
			$arg_array = array(	'goodsChoiceID' => $choice_id,
								'sortname'		=> 'goodsType',
								'sortorder'		=> 'asc');
				
			$result_detail = $commHandler->search($arg_array, 'promotion_selection_detail');
				
		}
		
		$preview_url = echo_preview_url();
		
		//页面输出数组
		$result_array = array(
				'page' => $page,
				'total' => $result_selection['message']['total'],
				'rows' => array()
		);
		
		if ($result_selection['code'] == 200 && !empty($result_selection['message']['data'])){
			foreach ($result_selection['message']['data'] as $selectionList){
		
				if ($selectionList['useFlg'] == 0){
					$useFlg = '禁用';
				}elseif ($selectionList['useFlg'] == 1){
					$useFlg = '启用';
				}
		

				$result_array['rows'][] = array(
						'id' => $selectionList['goodsChoiceID'],
						'cell' => array(
								'<a href="javascript:void(0);" onclick="source.detail('.$selectionList['goodsChoiceID'].');">修改</a>&nbsp;&nbsp;'.
								'<a href="javascript:void(0);" onclick="source.edit(this,'.$selectionList['goodsChoiceID'].');">'.$useFlg.'</a>&nbsp;&nbsp;'.
								'<a target="_blank" href="/shop/choice.php?uid=' . $selectionList['goodsChoiceID'] . '&'.$preview_url.'&subsite=shop">预览</a>',
                                '<a href="javascript:void(0);" onclick="source.info('.$selectionList['goodsChoiceID'].');">详情</a>',
								$selectionList['goodsChoiceID'],
								$selectionList['itemName'],
								$selectionList['goodsChoiceName'],
								$selectionList['goodsChoicePrice'],
								$selectionList['goodsCount'],
								$selectionList['points'],
								$selectionList['useStartDate'],
								$selectionList['useEndDate']
						)
				);
		
				if (!empty($choice_id) && !empty($result_detail) && $choice_id == $selectionList['goodsChoiceID']){
					$result_array['rows'][] = array(
							'id' => '',
							'cell' => array(
									'',
									'商品编号',
									'商品名称',
									'商品号',
									'网购号',
									'主网购号',
									'商品规格',
									'选购数量'
							)
					);
					foreach ($result_detail as $item){
						$result_array['rows'][] = array(
								'id' => '',
								'cell' => array(
										'',
										'&nbsp;&nbsp;'.$item->product_code,
										'&nbsp;&nbsp;'.$item->goodsName,
										$item->goodsID,
										$item->postID,
										$item->mainPostID,
										$item->goodsSpec,
										$item->goodsCount
								)
						);
					}
				}
		
			}
		}
  
		echo json_encode($result_array);
	}
	
	//根据product_code获得对应数据
	private function getGoodsInfo (){
		global $G_PGLOB;
		//商品编码
		$product_code = isset($_REQUEST['product_code']) ? trim($_REQUEST['product_code']) : '';
	
		if (empty($product_code)) {
			$result_array = array('code'=>400,'message'=>'请输入商品编号');
			echo json_encode($result_array);
			die();
		}
	
		$commHandler = new commHandler($G_PGLOB->DBCA);
		//查询条件：商品编号，useFlg（有库存），forsale（上架）
		$arg_array = array(	'product_code' 	=> $product_code,
				'useFlg'		=> 1,
				'forsale'		=> 1);
	
		$result_search = $commHandler->search($arg_array, 'goods_sale');
	
		if (empty($result_search)) {
			$result_array = array('code'=>400,'message'=>'该商品不存在或无库存或已下架');
			echo json_encode($result_array);
			die();
		}
		//传出的值，最终保存进dataCache中，且message数组中元素对应页面表格中第2列开始往后列
		$result_array = array(	'code'		=> 200,
				'message'	=> array(	'product_code'	=> $result_search[0]->product_code,
										'goodsName'		=> $result_search[0]->goodsName,
										'salePrice'		=> $result_search[0]->salePrice,
										'goodsCount'	=> '1',
										'postID'		=> $result_search[0]->postID,
										'mainPostID'	=> $result_search[0]->mainPostID,
										'goodsSpec'		=> $result_search[0]->spec,
										'goodsID'		=> $result_search[0]->goodsID));
	
		echo json_encode($result_array);
	}
	
	//编辑、新增超值选购
	private function editSelection () {
		global $G_PGLOB;
		
		//operFlg(2新增，3修改)
		$operFlg = isset($_REQUEST['operFlg']) ? $_REQUEST['operFlg'] : null;
		//主信息
		$mainInfo = isset($_REQUEST['mainInfo']) ? $_REQUEST['mainInfo'] : null;
		//套装商品
		$selectionInfo = isset($_REQUEST['selectionInfo']) ? $_REQUEST['selectionInfo'] : null;
	
		$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
	
		$importData = array('operFlg'       => $operFlg,
							'creator'       => $G_PGLOB->currentUserID,
							'editor'        => $G_PGLOB->currentUserID,
							'mainInfo' 		=> $mainInfo,
							'selectionInfo' => $selectionInfo);
	
		$result_edit = $promotionHandler->editSelection($importData);
	
		echo json_encode($result_edit);
	}
	
	//启用、禁用
	private function changeUseFlg () {
		global $G_PGLOB;
	
		$goodsChoiceID = isset($_REQUEST['goodsChoiceID']) ? $_REQUEST['goodsChoiceID'] : null;
	
		$changeFlg = isset($_REQUEST['changeFlg']) ? $_REQUEST['changeFlg'] : null;
	
		if (empty($goodsChoiceID) || !in_array($changeFlg, array(0,1))){
			$return_arr = array('code'=>400, 'message'=>'页面传值错误');
			echo json_encode($return_arr);
			die();
		}
	
		try {
				
			$selection_obj = new entity('promotion_selection');
			$selection_obj->goodsChoiceID = $goodsChoiceID;
			$selection_obj->useFlg = $changeFlg;
			if (!$G_PGLOB->DBCA->fetch($selection_obj)){
				$return_arr = array('code'=>400, 'message'=>'您的页面已过期，请刷新页面');
				echo json_encode($return_arr);
				die();
			}
			$selection_obj->useFlg = ($changeFlg == 1) ? 0 : 1;
				
			$msg = ($changeFlg == 1) ? '该选购已禁用' : '该选购已启用';
				
			$G_PGLOB->DBCA->save($selection_obj);
				
			$return_arr = array('code'=>200, 'message'=>$msg);
			echo json_encode($return_arr);
				
		}catch (Exception $e){
			$msg = $e->getMessage();
			$return_arr = array('code'=>500, 'message'=>$msg);
			echo json_encode($return_arr);
		}
	
	}
	
	//删除套装
	private function deleteSelection () {
		global $G_PGLOB;
		
		$choiceIDArr = isset($_REQUEST['choiceIDArr']) ? $_REQUEST['choiceIDArr'] : null;
	
		$promotionHandler = new promotionHandler($G_PGLOB->DBCA);
	
		$result_del = $promotionHandler->deleteSelection($choiceIDArr);
	
		echo json_encode($result_del);
	}

    //根据套装号得到商品套装详情
    private function getSelectInfo (){
        global $G_PGLOB;

        if(!isset($_REQUEST['goodsChoiceID']) || !is_numeric($_REQUEST['goodsChoiceID']) ){
            die();
        }
        //促销类
        $promotionHandler = new promotionHandler($G_PGLOB->DBCA);

        $arg_array = array('goodsChoiceID'=>$_REQUEST['goodsChoiceID']);

        //得到超值选购商品
        $result_detail = $promotionHandler->searchSelectionDetailAll($arg_array);

        //页面输出数组
        $result_array = array(
            'page' => '',
            'total' => '',
            'rows' => array()
        );

        if (!empty($result_detail['message']['data'])){
            foreach ($result_detail['message']['data'] as $detailList){

                $result_array['rows'][] = array(
                    'id' => $detailList->postID,
                    'cell'=>array(
                        $detailList->product_code,
                        $detailList->goodsName,
                        $detailList->salePrice,
                        $detailList->goodsCount,
                    )
                );
            }
        }
        echo json_encode($result_array);
    }

	
}